package edu.vt.ece.rt.benchmark.yada;

import edu.vt.ece.rt.dstm.QObject;
import edu.vt.ece.rt.quorum.Resource;

public class Coordinate extends QObject{

	double x;
	double y;
	static double M_PI = 3.14;
	String id;
	
	
	Coordinate (String id, double x, double y) {
		this.x = x;
		this.y = y;
		this.id = id;
		Resource.putQObject(getId(), this);
	}

	Coordinate () {
	}

	@Override
	public QObject clone() {
		Coordinate clone = new Coordinate();
		clone.x = this.x;
		clone.y = this.y;
		clone.setId(getId());
		clone.setVersion(getVersion());
		clone.setCloneOwner(getCloneOwner());
		return clone;
	}

	@Override
	public String getId() {
		return id;
	}

	
	public double getX() {
		return x;
	}

	public double getY() {
		return y;
	}
	
	public void setId(String ID) {
		this.id = ID;
	}
	
	public void setX(double X){
		this.x = X;
	}

	public void setY(double Y){
		this.y = Y;
	}

	long
	coordinate_compare (Coordinate aPtr, Coordinate bPtr)
	{
	    if (aPtr.x < bPtr.x) {
	        return -1;
	    } else if (aPtr.x > bPtr.x) {
	        return 1;
	    } else if (aPtr.y < bPtr.y) {
	        return -1;
	    } else if (aPtr.y > bPtr.y) {
	        return 1;
	    }

	    return 0;
	}
	
	double
	coordinate_distance (Coordinate coordinatePtr, Coordinate aPtr)
	{
	    double delta_x = coordinatePtr.x - aPtr.x;
	    double delta_y = coordinatePtr.y - aPtr.y;

	    return Math.sqrt((delta_x * delta_x) + (delta_y * delta_y));
	}
	 
	double
	coordinate_angle (Coordinate aPtr, Coordinate bPtr, Coordinate cPtr)
	{
	    Coordinate delta_b = new Coordinate();
	    Coordinate delta_c = new Coordinate();
	    double distance_b;
	    double distance_c;
	    double numerator;
	    double denominator;
	    double cosine;
	    double radian;

	    delta_b.x = bPtr.x - aPtr.x;
	    delta_b.y = bPtr.y - aPtr.y;

	    delta_c.x = cPtr.x - aPtr.x;
	    delta_c.y = cPtr.y - aPtr.y;

	    numerator = (delta_b.x * delta_c.x) + (delta_b.y * delta_c.y);

	    distance_b = coordinate_distance(aPtr, bPtr);
	    distance_c = coordinate_distance(aPtr, cPtr);
	    denominator = distance_b * distance_c;

	    cosine = numerator / denominator;
	    radian = Math.acos(cosine);

	    return (180.0 * radian / M_PI);
	}
	void
	coordinate_print (Coordinate coordinatePtr)
	{
	    System.out.println(coordinatePtr.x + " , "+ coordinatePtr.y);
	}
	
	void
	printAngle (Coordinate coordinatePtr, Coordinate aPtr, Coordinate bPtr,
	            double expectedAngle)
	{
	    double angle = coordinate_angle(coordinatePtr, aPtr, bPtr);

//	    System.out.println(
//	           aPtr.x ,aPtr.y,
//	           coordinatePtr.x, coordinatePtr.y,
//	           bPtr.x, bPtr.y,
//	           angle);
//
	    assert((angle - expectedAngle) < 1e-6);
	}


}
